package com.chenzp.rocketmq.batch;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname BatchProducer
 * @Description
 * @Version 1.0.0
 * @Date 2025-06-13 15:39
 * @Created by ncs
 */
@Slf4j
public class BatchProducer {

    @SneakyThrows
    public static void main(String[] args) {
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("my-group-producer");
        defaultMQProducer.setNamesrvAddr("192.168.125.102:9876");
        defaultMQProducer.start();
        List<Message> messages = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Message message = new Message("BatchTopic", ("批量消息" + i ).getBytes());
            messages.add(message);
        }
        SendResult sendResult = defaultMQProducer.send(messages);
        log.info("批量消息发送结果{}", sendResult);
        defaultMQProducer.shutdown();
    }
}
